package com.cn.controller;

import com.cn.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/data")
public class DataServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 模拟从数据库获取数据
        List<User> users = new ArrayList<>();
        users.add(new User(1, "张三", 25));
        users.add(new User(2, "李四", 30));
        users.add(new User(3, "王五", 28));

        // 将数据存储到 request 域中
        req.setAttribute("users", users);

        // 转发到 JSP 页面
        //req.getRequestDispatcher("/data.jsp").forward(req, resp);
        req.getRequestDispatcher("/data2.jsp").forward(req, resp);

        //req.getRequestDispatcher("/el-demo.jsp").forward(req, resp);
    }
}
